import Reqwest from '../../utils/Reqwest';
import {BuBean} from './BuBean';
import {base, assign, redirect}  from '../base/Base';
import {ResponseDto} from '../base/BaseDTO';
declare let GLOBAL: any;

export class BuController {
    path = `${GLOBAL.baseApiUrl}/bu/api`;

    /**
     * searchBu
     * 查询BU列表
     * @param {BuBean<any>} [params] 
     * @param {any} [options={}] 
     * @returns {Promise<any>} 
     * 
     * @memberOf BuController
     */
    searchBu(params?: any, options = {}): Promise<any> {
        return new Reqwest(assign({
            url: `${this.path}/list.jhtml`,
            method: 'get',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    /**
     * getBu
     * 根据ID查询BU信息
     * @param {{
     *         id:number
     *     }} params 
     * @param {any} [options={}] 
     * @returns {Promise<BuBean<any>>} 
     * 
     * @memberOf BuController
     */
    getBu(params: {
        id:number
    }, options = {}): Promise<ResponseDto<BuBean<any>>> {
        return new Reqwest(assign({
            url: `${this.path}/view.jhtml`,
            method: 'get',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    /**
     * editBu
     * 修改BU 
     * @param {BuBean<any>} params 
     * @param {any} [options={}] 
     * @returns {Promise<BuBean<any>>} 
     * 
     * @memberOf BuController
     */
    editBu(params: BuBean<any>, options = {}): Promise<ResponseDto<any>> {
        return new Reqwest(assign({
            url: `${this.path}/edit.jhtml`,
            method: 'post',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(params) || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    /**
     * addBu
     * 新增BU
     * @param {BuBean<any>} params 
     * @param {any} [options={}] 
     * @returns {Promise<BuBean<any>>} 
     * 
     * @memberOf BuController
     */
    addBu(params: BuBean<any>, options = {}): Promise<ResponseDto<any>> {
        return new Reqwest(assign({
            url: `${this.path}/add.jhtml`,
            method: 'post',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(params) || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    /**
     * deleteBu
     * 删除BU
     * @param {{
     *         id: number
     *     }} [params] 
     * @param {any} [options={}] 
     * @returns {Promise<any>} 
     * 
     * @memberOf BuController
     */
    deleteBu(params?: {
        id: number
    }, options = {}): Promise<ResponseDto<any>> {
        return new Reqwest(assign({
            url: `${this.path}/delete.jhtml`,
            method: 'get',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }
}